package com.yanqu.road.dao.impl.unioncampwar;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.unioncampwar.UserUnionCampWarData;
import com.yanqu.road.entity.unioncampwar.patrons.UnionCampWarLockPatronsData;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionCampWarLockPatronsDaoImpl extends TempDataObjectDao<UnionCampWarLockPatronsData> {

    public static String getUnionCampWarLockPatronsDataSql = "select * from `t_u_union_camp_war_lock_patrons`";

    public Map<String, List<UnionCampWarLockPatronsData>> getUnionCampWarLockPatronsDataMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, List<UnionCampWarLockPatronsData>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUnionCampWarLockPatronsDataSql);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionCampWarLockPatronsData data = parse(rs);
                List<UnionCampWarLockPatronsData> list = dataMap.get(data.getUnionId());
                if (list == null) {
                    list = new ArrayList<>();
                    dataMap.put(data.getUnionId(), list);
                }
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateUnionCampWarLockPatronsDataSql = "update `t_u_union_camp_war_lock_patrons` set updateTime=?,level=?,skin=?,star=?,promotionId=?,quality=?,ability=? where unionId=? and userId=? and patronsId=?";

    @Override
    protected boolean updateData(UnionCampWarLockPatronsData unionCampWarLockPatronsData) {
        return addData(unionCampWarLockPatronsData);
    }

    public static String insertUnionCampWarLockPatronsDataSql = "replace into `t_u_union_camp_war_lock_patrons`(unionId,userId,updateTime,patronsId,level,skin,star,promotionId,quality,ability) values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UnionCampWarLockPatronsData unionCampWarLockPatronsData) {
        unionCampWarLockPatronsData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, unionCampWarLockPatronsData.getUnionId()));
        para.put(i++, new DbParameter(Types.BIGINT, unionCampWarLockPatronsData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, unionCampWarLockPatronsData.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarLockPatronsData.getPatronsId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarLockPatronsData.getLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarLockPatronsData.getSkin()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarLockPatronsData.getStar()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarLockPatronsData.getPromotionId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarLockPatronsData.getQuality()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionCampWarLockPatronsData.getAbility().toString()));
        if (getDb().executeNoneQuery(insertUnionCampWarLockPatronsDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UnionCampWarLockPatronsData parseData(ResultSet rs) throws SQLException {
        UnionCampWarLockPatronsData unionCampWarLockPatronsData = new UnionCampWarLockPatronsData();
        unionCampWarLockPatronsData.setUnionId(rs.getString("unionId"));
        unionCampWarLockPatronsData.setUserId(rs.getLong("userId"));
        unionCampWarLockPatronsData.setUpdateTime(rs.getLong("updateTime"));
        unionCampWarLockPatronsData.setPatronsId(rs.getInt("patronsId"));
        unionCampWarLockPatronsData.setLevel(rs.getInt("level"));
        unionCampWarLockPatronsData.setSkin(rs.getInt("skin"));
        unionCampWarLockPatronsData.setStar(rs.getInt("star"));
        unionCampWarLockPatronsData.setPromotionId(rs.getInt("promotionId"));
        unionCampWarLockPatronsData.setQuality(rs.getInt("quality"));
        unionCampWarLockPatronsData.setAbility(new BigInteger(rs.getString("ability")));

        return unionCampWarLockPatronsData;
    }

}
